package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysCustomer;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.CustomerMapper;
import com.ruoyi.system.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    HttpServletRequest request;

    @Override
    public List<SysCustomer> selectCustomerList(SysCustomer customer) {
            return customerMapper.selectAllCustomerList(customer);
    }

    public List<SysCustomer> selectCustomerListByUserName(SysCustomer customer){
        return  customerMapper.selectCustomerListByUserName(customer);
    }

    @Override
    public int importCustomer(List<SysCustomer> customerList,String createBy) {
        Date date = new Date();
        if (StringUtils.isNull(customerList) || customerList.size() == 0)
        {
            throw new ServiceException("导入客户数据不能为空！");
        }
        for (SysCustomer customer:customerList){
            if (StringUtils.isEmpty(customer.getName())||
            StringUtils.isEmpty(customer.getProvince())||
                    StringUtils.isEmpty(customer.getCity())||
                    StringUtils.isEmpty(customer.getUserName())||
                    customer.getStatus() == null){
                throw new ServiceException("客户名称、所在省市、客户归属、状态不能为空！");
            }
            if (StringUtils.isEmpty(customer.getMobile())&&
                    StringUtils.isEmpty(customer.getTel())&&
                    StringUtils.isEmpty(customer.getWechat())&&
                    StringUtils.isEmpty(customer.getEmail())){
                throw new ServiceException("手机、座机、微信、邮箱至少有一种联系方式");
            }
            customer.setCreateTime(date);
            customer.setCreateBy(createBy);
        }
        return  customerMapper.importCustomerList(customerList);
    }

    @Override
    public SysCustomer selectCustomerById(Long customerId) {
        return customerMapper.selectCustomerById(customerId);
    }

    @Override
    public int updateCustomer(SysCustomer customer) {
        return customerMapper.updateCustomer(customer);
    }
}
